package com.appcan.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 记录代码执行过程
 * 
 */
public class RunTimeLogger {
	private RunTimeLogger(){}
	
	private static org.apache.log4j.Logger getLogger(Class c){
		return org.apache.log4j.Logger.getLogger(c);
	}
	/**
	 * 记录代码运行时的�异常��信�?
	 * @param c  记录日志的类
	 * @param msg 日志内容
	 */
	public static void info(Class c, String msg){
		getLogger(c).info(msg);
	}
	/**
	 * 记录代码运行时的�?��信息
	 * @param o  记录日志的对�?
	 * @param msg 
	 */
	public static void info(Object o, String msg){
		getLogger(o.getClass()).info(msg);
	}
	/**
	 * 记录代码运行时的调试信息
	 * @param o  记录日志的对�?
	 * @param msg
	 */
	public static void debug(Object o, String msg){
		getLogger(o.getClass()).debug(msg);
	}
	public static void debug(Class c, String msg){
		getLogger(c).debug(msg);
	}
	/**
	 * 记录代码运行时的调试信息,信息是打印obj的属�?
	 * @param o  记录日志的对�?
	 * @param obj
	 */
	public static void debug(Object o, Object obj){
		getLogger(o.getClass()).debug(getObjectString(obj));
	}
	public static void debug(Class c, Object obj){
		getLogger(c).debug(getObjectString(obj));
	}
	/**
	 * 仅提供打印属性为�?��数据类型的对�?
	 * @param obj
	 * @return
	 */
	private static String getObjectString(Object obj){
		StringBuffer ret = new StringBuffer(obj.getClass().getName());
		ret.append("{");
		Class c = obj.getClass();
		Method [] methods = c.getMethods();
		Field [] fields = c.getDeclaredFields();
		Field [] publicFields = c.getFields();
		try{
			for(Method method : methods){
				String name = method.getName();
				for(Field field : fields){
					int dot = name.toLowerCase().lastIndexOf(field.getName().toLowerCase());				
					if( dot >= 0){
						String head = name.substring(0, dot);
						String end  = name.substring(dot);
						if(end.compareToIgnoreCase(field.getName()) == 0){
							if((head.length() > 0 && head.compareToIgnoreCase("get") == 0)									
							   ||(head.length() == 0 && end.toLowerCase().startsWith("is"))){
								Object [] params = null;
								ret.append(field.getName() + "=" + method.invoke(obj, params).toString() + ";");
								break;
							}
						}
					}
				}
			}
			for(Field field : publicFields){
				ret.append(field.getName() + "=" + field.get(obj).toString() + ";");
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ret.append("}");
		return ret.toString();
	}
	/**
	 * 记录代码运行时的警告信息
	 * @param o  记录日志的对�?
	 * @param msg
	 */
	public static void warn(Object o, String msg){
		getLogger(o.getClass()).warn(msg);
	}
	public static void warn(Class c, String msg){
		getLogger(c).warn(msg);
	}
	/**
	 * 记录代码运行时的错误信息
	 * @param o  记录日志的对�?
	 * @param msg
	 */
	public static void error(Object o, String msg){
		getLogger(o.getClass()).error(msg);
	}
	public static void error(Class c, String msg){
		getLogger(c).error(msg);
	}
	/**
	 * 代码运行时的错误信息
	 * @param o  记录日志的对�?
	 * @param msg
	 * @param t 异常
	 */
	public static void error(Object o, String msg, Throwable t){
		getLogger(o.getClass()).error(msg, t);
	}
	public static void error(Class c, String msg, Throwable t){
		getLogger(c).error(msg, t);
	}	
}
